package com.rasou.jdk;

import org.geektimes.interceptor.ChainableInvocationContext;

import javax.interceptor.InvocationContext;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    private final Object target;
    private final Object[] interceptors;

    public MyInvocationHandler(Object target, Object[] interceptors) {
        this.target = target;
        this.interceptors = interceptors;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        InvocationContext context = new JDKInvocationContext(target, method, proxy, args);
        ChainableInvocationContext chainableInvocationContext = new ChainableInvocationContext(context, interceptors);
        return chainableInvocationContext.proceed();
    }
}
